package com.laowang.project.system.service.impl;

import java.util.List;

import com.laowang.common.exception.CustomException;
import com.laowang.project.system.domain.WxUser;
import com.laowang.project.system.service.WxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.laowang.project.system.mapper.MyCyhParNowNumMapper;
import com.laowang.project.system.domain.MyCyhParNowNum;
import com.laowang.project.system.service.MyCyhParNowNumService;

/**
 * 合伙人当前数量Service业务层处理
 *
 * @author laowang
 * @date 2024-04-03
 */
@Service
public class MyCyhParNowNumServiceImpl implements MyCyhParNowNumService {
    @Autowired
    private MyCyhParNowNumMapper myCyhParNowNumMapper;

    @Autowired
    private WxUserService wxUserService;
    /**
     * 查询合伙人当前数量
     *
     * @param id 合伙人当前数量ID
     * @return 合伙人当前数量
     */
    @Override
    public MyCyhParNowNum selectMyCyhParNowNumById(Integer id) {
        return myCyhParNowNumMapper.selectMyCyhParNowNumById(id);
    }

    /**
     * 查询合伙人当前数量列表
     *
     * @param myCyhParNowNum 合伙人当前数量
     * @return 合伙人当前数量
     */
    @Override
    public List<MyCyhParNowNum> selectMyCyhParNowNumList(MyCyhParNowNum myCyhParNowNum) {
        return myCyhParNowNumMapper.selectMyCyhParNowNumList(myCyhParNowNum);
    }

    /**
     * 新增合伙人当前数量
     *
     * @param myCyhParNowNum 合伙人当前数量
     * @return 结果
     */
    @Override
    public int insertMyCyhParNowNum(MyCyhParNowNum myCyhParNowNum) {
        Integer userId = myCyhParNowNum.getUserId();
        if (userId== null){
            throw new CustomException("用户id 不能为空");
        }
        WxUser wxUser = wxUserService.selectWxUserById(userId);
        if (  wxUser == null || wxUser.getIsPartner() == null || wxUser.getPartnerVip() == null || !wxUser.getPartnerVip().equals(1) ){
            throw new CustomException("用户非合伙人");
        }
        MyCyhParNowNum myCyhParNowNum1 =  myCyhParNowNumMapper.selectByUserId(userId);
        if (myCyhParNowNum1 !=null){
            throw new CustomException("配置文件已存在");
        }
        return myCyhParNowNumMapper.insertMyCyhParNowNum(myCyhParNowNum);
    }

    /**
     * 修改合伙人当前数量
     *
     * @param myCyhParNowNum 合伙人当前数量
     * @return 结果
     */
    @Override
    public int updateMyCyhParNowNum(MyCyhParNowNum myCyhParNowNum) {
        Integer userId = myCyhParNowNum.getUserId();
        if (userId== null){
            throw new CustomException("用户id 不能为空");
        }
        WxUser wxUser = wxUserService.selectWxUserById(userId);
        if (  wxUser == null || wxUser.getIsPartner() == null || wxUser.getPartnerVip() == null || !wxUser.getPartnerVip().equals(1) ){
            throw new CustomException("用户非合伙人");
        }
        return myCyhParNowNumMapper.updateMyCyhParNowNum(myCyhParNowNum);
    }

    /**
     * 批量删除合伙人当前数量
     *
     * @param ids 需要删除的合伙人当前数量ID
     * @return 结果
     */
    @Override
    public int deleteMyCyhParNowNumByIds(Integer[] ids) {
        return myCyhParNowNumMapper.deleteMyCyhParNowNumByIds(ids);
    }

    /**
     * 删除合伙人当前数量信息
     *
     * @param id 合伙人当前数量ID
     * @return 结果
     */
    @Override
    public int deleteMyCyhParNowNumById(Integer id) {
        return myCyhParNowNumMapper.deleteMyCyhParNowNumById(id);
    }

    @Override
    public MyCyhParNowNum selectByUserId(Integer userId){
        return myCyhParNowNumMapper.selectByUserId(userId);
    }
}
